package com.analytical.www.fargment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.analytical.www.MainActivity
import com.analytical.www.R
import com.analytical.www.adapter.AdapterSampleInput
import com.analytical.www.base.FragmentBase
import com.analytical.www.bean.BeanSample
import com.analytical.www.databinding.FragTestResultBinding
import com.analytical.www.pop.PopInputSample
import com.analytical.www.util.StringUtils.toast

/**
 *@Auther Shawn
 *创建时间：2023/12/24 12:34
 */
class FragTestResult:FragmentBase(), AdapterSampleInput.ItemClickListener,
    PopInputSample.IActionListener {
    private lateinit var binding:FragTestResultBinding
    private  var mData:MutableList<BeanSample> = mutableListOf()
    private var selectItem:BeanSample? = null

    private val mAdapter by lazy {
        val value = AdapterSampleInput(requireContext(),this)
        value
    }

    private val popInputSample by lazy {
        val value = PopInputSample(requireContext())
        value.listener = this
        value
    }

    override fun getLayoutId(): Int {
        return R.layout.frag_test_result
    }

    override fun lazyLoad() {
        initRecycleData()
        binding = FragTestResultBinding.bind(contentView)
        mAdapter.mData = mData
        binding.rlSample.apply {
            layoutManager = LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
            adapter = mAdapter
        }
        mAdapter.notifyDataSetChanged()
    }

    private fun initRecycleData() {
        mData.add(BeanSample("测试结果 2022/2/3"))
        mData.add(BeanSample("测试结果 2022/5/3"))
        mData.add(BeanSample("正五十烷 测试日志"))
        mData.add(BeanSample("测试结果 2022/7/22"))
        mData.add(BeanSample("测试结果 2022/8/11"))
        mData.add(BeanSample("测试结果 2022/2/3"))
        mData.add(BeanSample("汽油 测试日志"))
        mData.add(BeanSample("甲醇汽油 测试日志"))
        mData.add(BeanSample("汽油 202312a 测试日志"))
        mData.add(BeanSample("正十六烷 测试日志"))
    }

    override fun getData() {
        if (context is MainActivity) {
            (context as MainActivity).changeTitle("样品名称输入")
        }
    }

    override fun initClick() {
        binding.tvSearch.setOnClickListener {
            popInputSample.show(binding.root)
        }
        binding.ivUp.setOnClickListener {
            if (mAdapter.selPos < 0) {
                mAdapter.selPos = mAdapter.itemCount - 1
                binding.rlSample.scrollToPosition(mAdapter.selPos)
            } else {
                mAdapter.selPos -= 1
            }
        }
        binding.ivDown.setOnClickListener {
            if (mAdapter.selPos == mAdapter.itemCount - 1) {
                mAdapter.selPos = 0
                binding.rlSample.scrollToPosition(mAdapter.selPos)
            } else {
                mAdapter.selPos += 1
            }
        }
        binding.ivDelete.setOnClickListener {
            if (mAdapter.selPos >= 0 && mAdapter.selPos < mAdapter.itemCount) {
                mAdapter.mData.removeAt(mAdapter.selPos)
                mAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun onKeyCodeBack(): Boolean {
        return false
    }

    private fun close() {
        selectItem?.let {
            vmData?.taskLiveData?.postValue(it)
        }
        if (context is MainActivity) {
            (context as MainActivity).removeFullScreenFrag(FragTestResult::class.java.name)
        }
    }

    override fun click(position: Int) {
        selectItem = mData[position]
        mAdapter.selPos = position
    }

    override fun onActionOk(input: String) {
        if (input.isNotEmpty()) {
            input.toast()
        }
    }

    override fun onActionNo() {

    }

    companion object{
        fun newInstance(): FragTestResult {
            val args = Bundle()
            val fragment = FragTestResult()
            fragment.arguments = args
            return fragment
        }
    }
}